<?php
// unneeded session_start();

class Setup_data_model extends CI_Model{
    
    
    function __construct()
    {
        parent::__construct();
    }
////////////////////////////////////////////////////////////////////////////////////////////////
    
    public function create_regions($region_data) {
        
        $new_region = new ORM\Dynasties2\Regions;
        
        $new_region -> setRegionName($region_data['name']);
        $new_region -> setWhichWorld($region_data['world']);
        $new_region -> setLoyalty($region_data['loyalty']);
        $new_region -> setTaxIncome($region_data['taxincome']);
        $new_region -> setInfrastructure($region_data['infrastructure']);
        $new_region -> setMaxSoldiers($region_data['maxSoldiers']);
        $new_region -> setAvailSoldiers($region_data['maxSoldiers']);
        $new_region -> setCoordX('0');
        $new_region -> setCoordY('0');
        
        $this->doctrine->em->persist($new_region);
    
    
    }   ////// end of public function create_regions
////////////////////////////////////////////////////////////////////////////////////////////////
    
    public function destroy_regions() {
        
        $query = $this->doctrine->em->createQuery("SELECT u 
                FROM ORM\Dynasties2\Regions u
                ");
        $region = $query->getResult();
        
        foreach ($region as $row) {
            $this->doctrine->em->remove($row);
        }
        
        $this->doctrine->em->flush();
    
    
    }   ////// end of public function create_regions
////////////////////////////////////////////////////////////////////////////////////////////////
    
    public function commit_new_family($which_user) {
        
        //echo "-- " . $which_user . " -- </br>";
        
        //need some session security here?
        
        //$_SESSION["user"]
        
        $new_family = new ORM\Dynasties2\Families;
        $new_family->setFamilyName($this->input->post('family_name'));
        $new_family->setWhichUser($which_user);
        $new_family->setCreated(new DateTime());
        
        $this->doctrine->em->persist($new_family);
        $this->doctrine->em->flush();
        
        //echo $new_family->getID();
        $_SESSION['active_family'] = $new_family->getID();
        
        $this->load->view('register_success_view');
        
        
    
    }   ////// end of public function commit_new_family
////////////////////////////////////////////////////////////////////////////////////////////////
    
    public function commit_character_to_play() {
        
        $user_id = $_SESSION["user"];
        $which_character = $this->input->post('characterChoice');
        $character_data = $this->doctrine->em->find('ORM\Dynasties2\Characters', $which_character);
        
        $character_data->setPlayerControlled($user_id);
        $character_data->setUseAi('0');
        $character_data->setLastLoginTurn('0');
               
        $this->doctrine->em->flush();
        
    }  ////// end of public function commit_character_to_play
////////////////////////////////////////////////////////////////////////////////////////////////
    
    public function commit_new_character_to_db($character_data) {
        
        ////////////
        //echo $name;
        //$whichfamily;
        //$bornturn;
        //$deathturn;
        //$marriedturn;
        //$marriedto;
        //$isfemale;
        //$fathersID;
        //$religion;
        //$successionorder;
        //print_r($character_data);
        ////////////
        
        
        $new_character = new ORM\Dynasties2\Characters;
        $new_character->setCharacterName($character_data['name']);
        $new_character->setWhichFamily($character_data['whichfamily']);
        $new_character->setBornDate($character_data['bornturn']);
        $new_character->setDeathDate($character_data['deathturn']);
        $new_character->setMarriedDate($character_data['marriedturn']);
        $new_character->setMarriedTo($character_data['marriedto']);
        $new_character->setIsFemale($character_data['isfemale']);
        $new_character->setFathersId($character_data['fathersID']);
        $new_character->setReligion($character_data['religion']);
        $new_character->setSuccessionOrder($character_data['successionorder']);
        $this->doctrine->em->persist($new_character);
        
                
        $this->doctrine->em->flush();
        
        $new_char_id = $new_character->getID();
        //echo "</br>New char ID is " . $new_char_id;
        
        
        $tree_data = new ORM\Dynasties2\Familytree;
        $tree_data->setChildId($new_char_id);
        $tree_data->setFatherId($character_data['fathersID']);
        $tree_data->setMotherId($character_data['mothersID']);
        $tree_data->setIsMatrilineal('0');
        $this->doctrine->em->persist($tree_data);
        $this->doctrine->em->flush();
        
        return $new_char_id;
        
        
    }   ////// end of public function commit_new_character_to_db
////////////////////////////////////////////////////////////////////////////////////////////////
    
    public function create_families($names) {
        
        $i = '99';
        
        while ($i >= '0') {
            
            $new_family = new ORM\Dynasties2\Families;
            
            $new_family->setFamilyName($names[$i]);
            $this->doctrine->em->persist($new_family);
            
            $i--;
        }
        
    }   ////// end of public function create_families
////////////////////////////////////////////////////////////////////////////////////////////////
    
    public function commit_new_character_to_db_v2($character_data) {
        
        
        
        
        $new_character = new ORM\Dynasties2\Characters;
        $new_character->setCharacterName($character_data['name']);
        $new_character->setWhichFamily($character_data['whichfamily']);
        $new_character->setBornDate($character_data['bornturn']);
        //$new_character->setDeathDate($character_data['deathturn']);
        $new_character->setMarriedDate($character_data['marriedturn']);
        $new_character->setMarriedTo($character_data['marriedto']);
        $new_character->setIsFemale($character_data['isfemale']);
        $new_character->setFathersId($character_data['fathersID']);
        $new_character->setMothersID($character_data['mothersID']);
        $new_character->setIsRuler($character_data['isruler']);
        $new_character->setPlayerControlled($character_data['playercontrolled']);
        $new_character->setUseAI($character_data['useai']);
        $new_character->setPrestige($character_data['prestige']);
        $new_character->setAmbition($character_data['ambition']);
        $new_character->setLeadership($character_data['leadership']);
        $new_character->setDiplomacy($character_data['diplomacy']);
        $new_character->setMoney($character_data['money']);
        $new_character->setReligion('0');
        //$new_character->setSuccessionOrder($character_data['successionorder']);
        $this->doctrine->em->persist($new_character);
        
           $this->doctrine->em->flush();     
        
        $new_char_id = $new_character->getID();
        //echo "</br>New char ID is " . $new_char_id;
        
        
        
        //$this->doctrine->em->flush();
        
        return $new_char_id;
        
        
    }   ////// end of public function commit_new_character_to_db_v2
////////////////////////////////////////////////////////////////////////////////////////////////
    
    public function destroy_characters() {
        
        $query = $this->doctrine->em->createQuery("SELECT u 
                FROM ORM\Dynasties2\Characters u
                ");
        $results = $query->getResult();
        
        foreach ($results as $row) {
            $this->doctrine->em->remove($row);
        }
        
        $this->doctrine->em->flush();
    
    
    }   ////// end of public function create_regions
////////////////////////////////////////////////////////////////////////////////////////////////
    
    public function create_titles($data) {
        
       $character_data = $this->doctrine->em->find('ORM\Dynasties2\Characters', $data['charid']);
       $region_data = $this->doctrine->em->find('ORM\Dynasties2\Regions', $data['region']);
            
        $new_title = new ORM\Dynasties2\Titles;

        $new_title->setCharacterLink($character_data);
        $new_title->setRegionLink($region_data);
        $new_title->setTitleType('1');
        $this->doctrine->em->persist($new_title);

        
        
    }   ////// end of public function create_families
////////////////////////////////////////////////////////////////////////////////////////////////
    
    public function destroy_titles() {
        
       
            
        $query = $this->doctrine->em->createQuery("SELECT u 
                FROM ORM\Dynasties2\Titles u
                ");
        $results = $query->getResult();
        
        foreach ($results as $row) {
            $this->doctrine->em->remove($row);
        }
        
        $this->doctrine->em->flush();
    
        
        
    }   ////// end of public function create_families
////////////////////////////////////////////////////////////////////////////////////////////////
    
    
    
    }